var opinionPrice;

/**
 * 设置结果接收对象
 * @param _opinionPrice
 */
function setModel(_opinionPrice) {
    if (_opinionPrice == undefined || !(_opinionPrice instanceof com.jf.crawl.model.OpinionPrice)) {
        throw new java.lang.Exception("opinionPrice 对象为空");
    }
    log.info("opinionPrice {}", _opinionPrice)
    opinionPrice = _opinionPrice;
}

function crawler() {
    var map = new LinkedHashMap();
    for (var i = 1; i < 5; i++) {
        eachWDW(i, map)
    }
    if (map != null) {
        for (var string in map) {
            var month = string;
            var price = map[string].trim().replace("%", "");
            if ("1".equals(month)) {
                opinionPrice.setThreeMonth(commonUtil.string2Decimal(price));
            } else if ("2".equals(month)) {
                opinionPrice.setTwoMonth(commonUtil.string2Decimal(price));
            } else if ("3".equals(month)) {
                opinionPrice.setThreeMonth(commonUtil.string2Decimal(price));
            } else if ("6".equals(month)) {
                opinionPrice.setSixMonth(commonUtil.string2Decimal(price));
            } else if ("9".equals(month)) {
                opinionPrice.setNineMonth(commonUtil.string2Decimal(price));
            } else if ("12".equals(month)) {
                opinionPrice.setOneYear(commonUtil.string2Decimal(price));
            } else if ("24".equals(month)) {
                opinionPrice.setTwoYear(commonUtil.string2Decimal(price));
            } else if ("36".equals(month)) {
                opinionPrice.setThreeYear(commonUtil.string2Decimal(price));
            }
        }
    }
    return true;
}

//微贷网
function eachWDW(pageNo, map) {
    var url = "https://www.weidai.com.cn/list/goodsList?type=0&periodType=0&page=" + pageNo + "&rows=10&goodsType=PACKAGE";
    var result = Get(url);
    var json = JSON.parseObject(result);
    if (json.containsKey("data")) {
        var data = json.getJSONObject("data");
        if (data.containsKey("data")) {
            var jsonArray = JSON.parseArray(data.getString("data"));
            for (var i = 0; i < jsonArray.size(); i++) {
                var eachData = jsonArray.getJSONObject(i);
                var goodsTitle = eachData.getString("goodsTitle");
                var openTime = eachData.getString("openTime");
                var baseRate = eachData.getString("baseRate");
                var status = eachData.getString("status");
                var month = eachData.getString("month");
                if (goodsTitle.contains("专享") || status.equals("CLOSED")) {
                    continue;
                }
                if(getToday(openTime)==getDay()){
                    if (!map.containsKey(month)) {
                        map.put(month, baseRate);
                    }
                }else{
                    log.info("当前产品非今天更新,{},{}",goodsTitle,openTime)
                }
            }
        }
    }
}

function getToday(openTime) {
    var date = new Date(Number(openTime));
    var day = date.getDate();
    return day;
}

function getDay() {
    var date = new Date();
    var day = date.getDate();
    return day;
}